//
//  GeneratorQRCodeVC.swift
//  二维码
//
//  Created by kimi on 16/9/25.
//  Copyright © 2016年 kimi. All rights reserved.
//

import UIKit
//导入框架
import CoreImage

class GeneratorQRCodeVC: UIViewController {
    
    @IBOutlet weak var nameLabel: UILabel!
    
    @IBOutlet weak var countryLable: UILabel!
    
    @IBOutlet weak var qRCodeImageView: UIImageView!
    
    @IBOutlet weak var waikuangView: UIView!
    
    @IBOutlet weak var touxiangImageView: UIImageView!
    
     fileprivate var resultImage:UIImage?
    

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        self.title = "我的二维码"
        let leftBarBtn = UIBarButtonItem(title: "", style: .plain, target: self,action: "backToPrevious")
        leftBarBtn.image = UIImage(named: "v2_goback")
    
        
    }
    //返回按钮点击响应
    func backToPrevious(){
        self.navigationController?.popViewController(animated: true)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

}


//override func didReceiveMemoryWarning() {
//    super.didReceiveMemoryWarning()
//}
//}
//
    func creatQRCode(_ message:String?, imageName:String?) -> Void {
        
        let str = message ?? ""

        //1.创建二维码滤镜
        
        let filter = CIFilter(name: "CIQRCodeGenerator")
        
        //5.1回复滤镜默认设置
        filter?.setDefaults()
        
        //2.设置滤镜输入数据
        //KVC
        let data = str.data(using: String.Encoding.utf8)
        
        //2.2设置二维码的纠错率
        filter?.setValue("M", forKey: "inputCorrectionLevel")
        
        filter?.setValue(data, forKey: "inputMessage")
        //3.从二维码滤镜里面，获取结果图片
        var image = filter?.outputImage
        
        //5.获取高清图片
        let transform = CGAffineTransform.init(scaleX: 20, y: 20)
        
        image =  image?.applying(transform)
        
        //3.1图片处理
        
        var resultImage = UIImage(ciImage: image!)
        
        //前景图片
        let center = UIImage(named: imageName!)
        //处理图片
        resultImage = getNewImage(resultImage, center: center!)
        
        self.resultImage = resultImage

    }
    
    override func viewDidLayoutSubviews() {
        
        super.viewDidLayoutSubviews()
        //4.显示图片
        qRCodeImageView.image = resultImage
        
        //切割图片
        waikuangView.layer.cornerRadius = 15
        waikuangView.layer.masksToBounds = true
        
        //切头像
        touxiangImageView.layer.cornerRadius = 15
        touxiangImageView.layer.masksToBounds = true
    }
    
    private func getNewImage(_ sourceImage:UIImage,center:UIImage) -> UIImage {
        let  size = sourceImage.size
        //1.开启土星上下文
        UIGraphicsBeginImageContext(size)
        //2.绘制大图片
        sourceImage.draw(in: CGRect.init(x: 0, y: 0, width: size.width, height: size.height))
        //3.绘制小图片
        let width:CGFloat = 80
        let height:CGFloat = 80
        
        let x:CGFloat = (size.width - width) * 0.5
        let y:CGFloat = (size.height - height) * 0.5

        center.draw(in: CGRect.init(x: x, y: y, width: width, height: height))
        //4.取出结果图片
        let resultImage = UIGraphicsGetImageFromCurrentImageContext()
        
        //5.关闭上下文
        UIGraphicsEndImageContext()
        //6.返回结果
        return resultImage!
    }
    
    
    func setViewInfo(name:String?,country:String?, headerImageName:String?) -> Void {
        loadViewIfNeeded()
        //设置名字
        nameLabel.text = name
        //设置城市
        countryLable.text = country
        //设置图片
        touxiangImageView.image = UIImage(named: headerImageName!)
    
    }
    
}
